<template>
  <el-row style="width:99%" class="bianju" type="flex" justify="space-between">
    <el-col :span="20">
      <span>{{ nodeDate.name }}</span>
    </el-col>
    <el-col :span="2">
      <span>{{ nodeDate.manager }}</span>
    </el-col>
    <el-col :span="2">
      <el-dropdown @command="handleCommand">
        <span class="el-dropdown-link" @click.stop>
          下拉菜单<i class="el-icon-arrow-down el-icon--right" />
        </span>
        <template #dropdown>
          <el-dropdown-menu slot="dropdown">
            <el-dropdown-item command="add">添加子部门</el-dropdown-item>
            <el-dropdown-item v-if="!root" command="edit">查看部门</el-dropdown-item>
            <el-dropdown-item v-if="!root" command="del">删除部门</el-dropdown-item>
          </el-dropdown-menu>
        </template>
      </el-dropdown>
    </el-col>
  </el-row>
</template>

<script>
import { delDepartment } from '@/api/department'
export default {
  props: {
    nodeDate: {
      type: Object,
      required: true
    },
    root: {
      type: Boolean,
      default: false
    }
  },
  methods: {
    async handleCommand(command) {
      // 点击删除部门
      if (command === 'del') {
        try {
          // 弹窗提醒是否确认删除此部门
          await this.$confirm('你确定要删除此部门吗', '温馨提示', {
            type: 'warning'
          })
        } catch {
          return
        }
        try {
          // 删除部门可能会删失败  如：默认不让删除的部门
          await delDepartment(this.nodeDate.id)
        } catch {
          return
        }
        this.$message.success('删除部门成功')
        this.$emit('getList')
      }
      // 添加部门
      if (command === 'add') {
        this.$emit('addFn', this.nodeDate)
      }
      // 查看部门
      if (command === 'edit') {
        this.$emit('edit', this.nodeDate)
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.bianju{
  margin: 10px;
  ::v-deep{
    .el-tree-node{
      margin: 5px;
    }
  }
}
</style>
